package controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import restaurant.Location;

/**
 * Servlet implementation class LocationsEdit
 */
@WebServlet("/Edit%20Locations")
public class LocationsEdit extends Locations {
    private static final long serialVersionUID = 201212030048L;

    @Override
    protected Map<String, String> getLinkMap() {
        return LinkData.getAdminLinkMap();
    }

    @Override
    protected String getDispatcherPage() {
        return "/WEB-INF/locations_edit.jsp";
    }

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {
        final String id = req.getParameter("id");
        final String name = req.getParameter("name");
        final String description = req.getParameter("description");
        final String phoneNumber = req.getParameter("phone");
        final String address = req.getParameter("address");
        final String city = req.getParameter("city");
        final String state = req.getParameter("state");
        final String zip = req.getParameter("zip");

        req.setAttribute("id", id);
        final List<Location> locations = new ArrayList<Location>();

        final EntityManager em = _emf.createEntityManager();

        em.getTransaction().begin();
        Location oldLocation = null;
        if (null != id && !(id.isEmpty())) {
            oldLocation = em.find(Location.class, Integer.parseInt(id));
        }
        if (null == oldLocation) {
            Location newLocation = new Location();
            newLocation.setName(name);
            newLocation.setDescription(description);
            newLocation.setPhoneNumber(phoneNumber);
            newLocation.setAddress(address);
            newLocation.setCity(city);
            newLocation.setState(state);
            newLocation.setZip(zip);
            try {
                newLocation.setSrcLink(LocationUrlBuilder.getSrcLink(newLocation));
                newLocation.setHrefLink(LocationUrlBuilder.getHrefLink(newLocation));
            } catch (final URISyntaxException urise) {
                // TODO: log exception, display error to user
                urise.printStackTrace();
            }
            em.persist(newLocation);
        } else {
            oldLocation.setName(name);
            oldLocation.setDescription(description);
            oldLocation.setPhoneNumber(phoneNumber);
            oldLocation.setAddress(address);
            oldLocation.setCity(city);
            oldLocation.setState(state);
            oldLocation.setZip(zip);
            try {
                oldLocation.setSrcLink(LocationUrlBuilder.getSrcLink(oldLocation));
                oldLocation.setHrefLink(LocationUrlBuilder.getHrefLink(oldLocation));
            } catch (final URISyntaxException urise) {
                // TODO: log exception, display error to user
                urise.printStackTrace();
            }
            em.merge(oldLocation);
        }
        em.getTransaction().commit();

        locations.addAll(em.createQuery("select l from Location l", Location.class).getResultList());
        em.close();

        req.setAttribute("locations", locations);

        req.setAttribute("namesToPages", getLinkMap());
        req.getRequestDispatcher(getDispatcherPage()).forward(req, resp);
    }
}
